<?php

use Phalcon\Paginator\Adapter\Model as PaginatorModel;

class MarketingController extends ControllerBase
{
    public function initialize()
    {
        parent::initialize();
    }

    public function taolijin_listAction(){
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $this->params['page'] = 1;
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $this->params['limit'] = 10;
        }
        $taolijinDb = new FTaolijin();
        $getlist = $taolijinDb->get_list($this->params);
        $result['items'] = $getlist;
        $result['total'] = $taolijinDb->get_list($this->params,'count');
        $this->ok($result);
    }

    public function taolijinAction(){
        $taolijinDb = new FTaolijin();
        if(!$this->params['goods_id'] || empty($this->params['goods_id'])){
            $this->fail(-1,'商品名称不能为空');
        }
        if(!$this->params['name'] || empty($this->params['name'])){
            $this->fail(-1,'淘礼金名称不能为空');
        }
        if(!$this->params['total_num'] || empty($this->params['total_num'])){
            $this->fail(-1,'淘礼金总个数不能为空');
        }
        if(!$this->params['per_face'] || empty($this->params['per_face'])){
            $this->fail(-1,'单个淘礼金面额不能为空');
        }
        if(!$this->params['user_total_win_num_limit'] || empty($this->params['user_total_win_num_limit'])){
            $this->fail(-1,'单用户累计中奖次数上限不能为空');
        }
        if(!$this->params['user_total_win_num_limit'] || empty($this->params['user_total_win_num_limit'])){
            $this->fail(-1,'单用户累计中奖次数上限不能为空');
        }
        if(!$this->params['user_total_win_num_limit'] || empty($this->params['user_total_win_num_limit'])){
            $this->fail(-1,'单用户累计中奖次数上限不能为空');
        }
        if(!$this->params['send_start_time'] || empty($this->params['send_start_time'])){
            $this->fail(-1,'发放开始时间不能为空');
        }
        if(!$this->params['send_end_time'] || empty($this->params['send_end_time'])){
            $this->fail(-1,'发放结束时间不能为空');
        }
        $goods_detail = $this->haodanku->goods_detail($this->params['goods_id']);
        if(isset($goods_detail['code']) == -1 || !$goods_detail){
           $this->fail(-1,$goods_detail['msg']);
        }
        $this->params['goods_name'] = $goods_detail['Title'];
        $this->params['goods_pic'] = $goods_detail['PicLogo'];
        $this->params['campaign_type'] = 'MKT';
        $taobao = new Taobao();
        $get_list = $taobao->taolijin($this->params);
        if(!$get_list['code']){
            $this->fail(-1,$get_list['msg']);
        }
        $this->params['goods_url'] = $get_list['data']->send_url;
        $this->params['mount'] = $get_list['data']->available_fee;
        $this->params['created_at'] = date('Y-m-d H:i:s');
        if($taolijinDb->save($this->params)){
            $freeGoodsDb = new Freegoods();
            $freeGoodsDb->goods_id =$goods_detail['GoodsId'];
            $freeGoodsDb->free_price = $this->params['per_face'];
            $freeGoodsDb->site='haodanku';
            $freeGoodsDb->types='free';
            $freeGoodsDb->title=$goods_detail['Title'];
            $freeGoodsDb->pic=$goods_detail['PicLogo'];
            $freeGoodsDb->counts = $this->params['total_num'];
            $freeGoodsDb->buyed =  $this->params['total_num'];
            $freeGoodsDb->coupon = $goods_detail['CouponMoney'];
            $freeGoodsDb->price = $goods_detail['EndPrice'];
            $freeGoodsDb->less = $this->params['total_num'];
            $freeGoodsDb->old_price = $goods_detail['EndPrice'];
            $freeGoodsDb->taolijin_price = $this->params['per_face'];
            $freeGoodsDb->send_start_time = date('Y-m-d H:i:s');
            $freeGoodsDb->status=1;
            $freeGoodsDb->created_at= date('Y-m-d H:i:s');
            $freeGoodsDb->taolijin_url = $get_list['data']->send_url;
            $freeGoodsDb->save();
            $this->ok([]);
        }else{
            $this->fail(-1,'保存失败');
        }
    }

    public function virtualorderAction()
    {
        $userStatus           = new UserStatus();
        $where['conditions'] = 'is_virtual = 1';
        $list                 = $userStatus->find($where)->toarray();
        if (!empty($list)) {
            $user = new User();
            foreach ($list as &$val) {

                $userInfo = $user->findFirstById($val['user_id']);
                if($userInfo) {
                    $val['phone'] = $userInfo->phone;
                }
            }
        }
        $this->ok($list);
    }

    public function getUserAction()
    {
        $user = new User();
        $info = $user->findFirstByPhone($this->params['phone']);
        if ($info) {
            $this->ok($info);
        } else {
            $this->fail(-1, '用户不存在!');
        }
    }

    public function setvirtualorderAction()
    {
        $user             = new UserStatus();
        $data             = $user->get_user($this->params['id']);
        $data->is_virtual = $this->params['is_virtual'];
        if ($data->save()) {
            $this->ok('成功');
        } else {
            $this->fail(-1, '操作失败!');
        }
    }

    /**
     * 短信会员列表
     */
    public function smsUserListAction()
    {
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page'] = 1;
        }
        if(!isset($this->params['limit']) || empty($this->params['limit'])){
            $this->params['limit'] = 10;
        }
        $user = new User();
        $list = $user->smsUserList($this->params);
        if(!empty($list['items'])){
            foreach ($list['items'] as $key => $val){
                if(!$val['pay_money']){
                    $list['items'][$key]['pay_money'] = 0;
                }
                if(!$val['order_count']){
                    $list['items'][$key]['order_count'] = 0;
                }
                if(!$val['pay_money_avg']){
                    $list['items'][$key]['pay_money_avg'] = 0;
                }
                if(!$val['order_create_time']){
                    $list['items'][$key]['order_create_time'] ='';
                }
            }
        }
        $this->ok($list);
    }

    public function smsModelListAction()
    {
        $this->jpush->templates();
    }
}
